import type { CartItem, PreOrder } from '@/types/cartItem'
import { http } from '@/utils/http'

//添加到购物车
export const postMemberCartAPI = (cart: { userId: number; skuId: number; goodsCount: number }) => {
  return http({
    method: 'POST',
    url: '/member/cart/addCart',
    data: cart,
  })
}

//获取用户购物车列表
export const getMemberCartAPI = (userId: number) => {
  return http<CartItem[]>({
    method: 'POST',
    url: '/member/cart/findCartGoodsList',
    data: userId + '', // 注意：这里需要把userId转为字符串
  })
}

//清空/删除购物车的商品
export const deleteMemberCartAPI = (ids: number[]) => {
  return http({
    method: 'POST',
    url: '/member/cart/delCart',
    data: ids,
  })
}

//修改购物车单品
export const putMemberCartAPI = (
  data: {
    id: number
    count?: number
    selected?: boolean
  },
  opr?: string,
) => {
  return http({
    method: 'GET',
    url: `/member/cart/${opr}`,
    data,
  })
}

//修改购物车列表中全选/全不选
export const putMemberCartSelectAPI = (data: { userId: number; selected: boolean }) => {
  return http({
    method: 'GET',
    url: '/member/cart/updateAllSelect',
    data,
  })
}

//获取预付订单信息
export const getPreOrderAPI = (userId: number) => {
  return http<PreOrder>({
    method: 'GET',
    url: '/member/cart/preOrder',
    data: { userId },
  })
}
